package com.qianli.cashmama.trade.dao.impl;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.api.Page;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.exception.common.ApplicationException;
import com.qianli.cashmama.trade.dao.TTradeDao;
import com.qianli.cashmama.trade.entity.TTradeEntity;
import com.qianli.cashmama.trade.vo.LoanAgainNotificationUserVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: sm
 * Date: 2017-07-20
 * Time: 19:47:54.188
 */

@Repository
public class TTradeDaoImpl extends AbstractBaseMapper<TTradeEntity> implements TTradeDao {
    private static final Logger logger = LoggerFactory.getLogger(TTradeDaoImpl.class);

    @Override
    public TTradeEntity selectOneByTradeNo(String tradeNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("tradeNo", tradeNo);
        return getSqlSession().selectOne(getStatement("selectOneByTradeNo"), map);
    }


    @Override
    public Integer updateByTradeNo(TTradeEntity tTradeEntity) {
        return getSqlSession().update(getStatement("updateByTradeNo"), tTradeEntity);
    }

    @Override
    public int countByUserCode(String userCode, String productCategory) {
        if (StringUtils.isEmpty(userCode)) {
            return 0;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("userCode", userCode);
        map.put("productCategory", productCategory);
        return getSqlSession().selectOne(getStatement("countByUserCode"), map);
    }

    @Override
    public List<TTradeEntity> selectListByCondition(String merchantCode, String userCode, Integer orderStatus) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderStatus", orderStatus);
        map.put("userCode", userCode);
        map.put("merchantCode", merchantCode);
        return getSqlSession().selectList(getStatement("selectListByCondition"), map);
    }

    @Override
    public TTradeEntity selectOneByUserCodeAndProductCategory(String productCategory, String userCode) {
        if (StringUtils.isEmpty(userCode)) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("productCategory", productCategory);
        map.put("userCode", userCode);
        return getSqlSession().selectOne(getStatement("selectOneByUserCodeAndProductCategory"), map);
    }

    @Override
    public List<TTradeEntity> selectListByUserCodeAndOrderStatus(Integer orderStatus, String userCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderStatus", orderStatus);
        map.put("userCode", userCode);
        return getSqlSession().selectList(getStatement("selectListByUserCodeAndOrderStatus"), map);
    }

    @Override
    public TTradeEntity selectOneActiveByUserCode(String userCode) {
        if (StringUtils.isEmpty(userCode)) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("userCode", userCode);
        return getSqlSession().selectOne(getStatement("selectOneActiveByUserCode"), map);
    }

    @Override
    public List<TTradeEntity> selectListByUserCodeAndProductCategory(String userCode, String productCategory, Page page, Boolean closed, Integer closedType) {
        if (StringUtils.isEmpty(userCode)) {
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("productCategory", productCategory);
        map.put("userCode", userCode);
        map.put("page", page);
        if (closed == null) {
            map.put("closed", 3);
        } else {
            map.put("closed", closed ? 1 : 0);
        }
        map.put("closedType", closedType);

        logger.info("selectListByUserCodeAndProductCategory map:{}", JSON.toJSON(map));
        page.setTotalCount(getSqlSession().selectOne(getStatement("countByUserCodeAndProductCategory"), map));

        return getSqlSession().selectList(getStatement("selectListByUserCodeAndProductCategory"), map);
    }

    @Override
    public List<TTradeEntity> selectListByUserCodeAndProductCategoryAndStatus(String userCode, String productCategory) {
        if (StringUtils.isEmpty(userCode)) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("productCategory", productCategory);
        map.put("userCode", userCode);
        return getSqlSession().selectList(getStatement("selectListByUserCodeAndProductCategoryAndStatus"), map);
    }

    @Override
    public List<TTradeEntity> selectListByUserCodes(List<String> userCodes) {
        if (CollectionUtils.isEmpty(userCodes)) {
            logger.info("===   88888888 ==,size:{}");
            return new ArrayList<>();
        }

        logger.info("===   88888888 ==,size:{}", userCodes.size());
        if (userCodes.size() > 1000) {
            logger.info("===   88888888  exception ==,size:{}", userCodes.size());
            throw new ApplicationException("more than 2000");
        }

        Map<String, Object> map = new HashMap<>();
        map.put("userCodes", userCodes);
        return getSqlSession().selectList(getStatement("selectListByUserCodes"), map);
    }

    @Override
    public List<TTradeEntity> selectListByUserCode(String userCode, Page page) {
        if (StringUtils.isEmpty(userCode)) {
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("userCode", userCode);
        map.put("page", page);

        return getSqlSession().selectList(getStatement("selectListByUserCode"), map);
    }

    @Override
    public List<TTradeEntity> selectListByUserCode(String userCode) {
        if (StringUtils.isEmpty(userCode)) {
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("userCode", userCode);

        return getSqlSession().selectList(getStatement("selectTradeListByUserCode"), map);
    }

    @Override
    public List<TTradeEntity> selectListByTradeNosAndPage(List<String> tradeNos, Page page) {
        if (CollectionUtils.isEmpty(tradeNos)) {
            logger.info("===   88888888999 ==");
            return new ArrayList<>();
        }

        logger.info("===   8888888899911 ==,size:{}", tradeNos.size());
        Map<String, Object> map = new HashMap<>();
        map.put("tradeNos", tradeNos);
        map.put("page", page);
        return getSqlSession().selectList(getStatement("selectListByTradeNosAndPage"), map);
    }


    @Override
    public List<TTradeEntity> selectListByUserCodesAndStatusAndTradeNo(List<String> userCodes, List<Integer> tradeStatusList, String tradeNo, String productCategory, Page
            page) {
        List<TTradeEntity> tradeEntityList = new ArrayList<>();


        if (CollectionUtils.isEmpty(userCodes)) {
            logger.info("===   8888888899911111 ==");
            return null;
        }

        logger.info("===   8888888899911111 ==,size:{}", userCodes.size());

        if (!StringUtils.isEmpty(tradeNo)) {
            TTradeEntity tradeEntity = selectOneByTradeNo(tradeNo);
            if (tradeEntity == null) {
                return null;
            }

            if ((tradeStatusList == null || tradeStatusList.contains(tradeEntity.getOrderStatus()))
                    && tradeEntity.getProductCategory().equals(productCategory)
                    && (CollectionUtils.isEmpty(userCodes) || userCodes.contains(tradeEntity.getUserCode()))) {
                tradeEntityList.add(tradeEntity);
            }

            return tradeEntityList;
        }

        Map<String, Object> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(userCodes)) {
            map.put("userCodes", userCodes);
        }
        map.put("page", page);
        map.put("tradeStatusList", tradeStatusList);
        map.put("productCategory", productCategory);

        if (page.getTotalCount() == null || page.getTotalCount() == 0) {
            Integer count = getSqlSession().selectOne(getStatement("countListByUserCodesAndStatus"), map);
            page.setTotalCount(count);
        }

        return getSqlSession().selectList(getStatement("selectListByUserCodesAndStatus"), map);
    }


    @Override
    public List<TTradeEntity> selectByOrderIds(List<Long> orderIds) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderIds", orderIds);
        return getSqlSession().selectList(getStatement("selectByOrderIds"), map);
    }


    @Override
    public List<TTradeEntity> selectByOrderStatus(Integer orderStatus) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderStatus", orderStatus);
        return getSqlSession().selectList(getStatement("selectByOrderStatus"), map);
    }

    @Override
    public List<TTradeEntity> selectListByTradeNos(List<String> tradeNos) {
        if (CollectionUtils.isEmpty(tradeNos)) {
            logger.info("===   888888889991111111111 ==");
            return null;
        }

        logger.info("===   8888888899911111 aadfa==,size:{}", tradeNos.size());

        Map<String, Object> map = new HashMap<>();
        map.put("tradeNos", tradeNos);
        return getSqlSession().selectList(getStatement("selectListByTradeNos"), map);
    }


    @Override
    public TTradeEntity selectOneByUserCodeAndProductCode(String productCode, String userCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("productCode", productCode);
        map.put("userCode", userCode);
        return getSqlSession().selectOne(getStatement("selectOneByUserCodeAndProductCode"), map);
    }

    @Override
    public List<LoanAgainNotificationUserVO> selectLoanAgainNotificationUser() {
        return getSqlSession().selectList(getStatement("selectLoanAgainNotificationUser"));
    }

    @Override
    public int countOrdersByUserCode(String userCode, Boolean closed) {
        Map<String, Object> map = new HashMap<>();
        map.put("productCategory", null);
        map.put("userCode", userCode);
        if (closed == null) {
            map.put("closed", 3);
        } else {
            map.put("closed", closed ? 1 : 0);
        }
        map.put("closedType", null);
        return getSqlSession().selectOne(getStatement("countByUserCodeAndProductCategory"), map);
    }

    @Override
    public int countCurrentDateOrdersByCategoryCode(String categoryCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("categoryCode", categoryCode);
        return getSqlSession().selectOne(getStatement("countCurrentDateOrdersByCategoryCode"), map);
    }

    @Override
    public List<TTradeEntity> getFirstPushWaittingList(Page page) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        return getSqlSession().selectList(getStatement("getFirstPushWaittingList"), map);
    }

    @Override
    public List<TTradeEntity> getSecondPushWaittingList(Page page) {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        return getSqlSession().selectList(getStatement("getSecondPushWaittingList"), map);
    }

    @Override
    public List<TTradeEntity> getThirdPushWaittingList(Page page) {

        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        return getSqlSession().selectList(getStatement("getThirdPushWaittingList"), map);
    }

    @Override
    public List<TTradeEntity> getListByProductCategory(String productCategory) {
        Map<String, Object> map = new HashMap<>();
        map.put("productCategory", productCategory);
        return getSqlSession().selectList(getStatement("getListByProductCategory"), map);
    }

    @Override
    public List<TTradeEntity> getOldUnpaidTradeListByMobile(String mobile) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        return getSqlSession().selectList(getStatement("getOldUnpaidTradeListByMobile"), map);
    }

    @Override
    public void copyInsert(TTradeEntity entity) {
        this.getSqlSession().insert(this.getStatement("copyInsert"), entity);
    }

    @Override
    public Integer getWhiteCount(String productCategory, String mobile, Integer type) {

        Map<String, Object> map = new HashMap<>();
        map.put("productCategory", productCategory);
        map.put("mobile", mobile);
        map.put("type", type);
        return getSqlSession().selectOne(getStatement("getWhiteCount"), map);
    }


}